package com.example.application.views.bookView;

import com.example.application.bean.BookBean;
import com.example.application.views.main.MainView;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Route(value = "books",layout = MainView.class)
@PageTitle("显示书籍信息")
public class BookView extends Div {

    private Grid<BookBean>  grid = new Grid<>(BookBean.class);

    Button button = new Button("添加");


    public BookView(){

        grid.setColumns("id","name","price","pic","num");
        //显示数据
        grid.setItems(showAllBooks());
        //添加
        button.addClickListener(e->{
            try(CloseableHttpClient httpclient = HttpClients.createDefault()){
                HttpPost post = new HttpPost("http://127.0.0.1:8888/ssm/book/add");
                //用来装分表单参数的
                List<NameValuePair> nvps = new ArrayList<>();
                nvps.add(new BasicNameValuePair("name", "hell httpClient"));
                nvps.add(new BasicNameValuePair("price", "100"));
                nvps.add(new BasicNameValuePair("pic", "123.jpg"));
                nvps.add(new BasicNameValuePair("num", "100"));
                post.setEntity(new UrlEncodedFormEntity(nvps));
                CloseableHttpResponse response = httpclient.execute(post);
                HttpEntity entity = response.getEntity();
                String json = EntityUtils.toString(entity,"utf-8");
                if(json.equals("ok")){
                    //重新发送请的请求(更新数据)
                    grid.setItems(this.showAllBooks());
                }

            }catch (Exception exception){

                exception.printStackTrace();
            }


        });

        this.add(button,grid);
    }

    //往后台发送请求去取数据
    private List<BookBean>  showAllBooks(){

        List<BookBean> list = null;

        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {

            //创建get请求
            HttpGet httpGet = new HttpGet("http://127.0.0.1:8888/ssm/book/showAll");
            //发送求
            CloseableHttpResponse response =  httpclient.execute(httpGet);
            //得到返回的结果
            HttpEntity entity = response.getEntity();
            //得到返回的json数据
           String json =  EntityUtils.toString(entity,"utf-8");

           // System.out.println(json+"=====================>>");
          //然后把json字符串转为集合
            ObjectMapper objectMapper = new ObjectMapper();
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class,BookBean.class);
            //把json字符串转为集合
            list =  objectMapper.readValue(json,javaType);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }


}
